<?php

// +----------------------------------------------------------------------
// | GuoRan100 [ 果然100 你的选择 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 扬州鲜生活电子商务有限公司 All rights reserved.
// +----------------------------------------------------------------------
// | Author: 曹梦龙 <138888611@qq.com> <http://www.guoran100.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Org\BMap;
/**
 * 后台地图控制器
 * @author 曹梦龙 <138888611@qq.com>
 */
class MapController extends AdminController {

    public function Area($areaname = "", $name = "", $phone = "", $warehouse = "", $points = "") {
        if (IS_POST) {
            $AreaData = array('areaName' => $areaname, 'name' => $name, 'phone' => $phone, 'warehouse' => $warehouse, 'points' => $points);
            $Area = D('Area');
            $result = $Area->create($AreaData);
            if ($result) {
                if (!$Area->add()) {
                    $this->error("新区域添加失败！");
                } else {
                    $this->success("新区域添加成功！", U('Area'));
                }
            } else {
                $this->error($Area->getError());
            }
        } else {
            $warehouselist = M('warehouse')->where("status=1")->select();
            $this->warehouselist = $warehouselist;
            $this->display();
        }
    }

    //区域编辑
    public function AreaEdit($id = "", $areaname = "", $name = "", $phone = "", $warehouse = "", $points = "") {
        $AreaData = array('areaName' => $areaname, 'name' => $name, 'phone' => $phone, 'warehouse' => $warehouse, 'points' => $points);
        $Area = D('Area');
        if ($Area->create($AreaData) && $Area->where(array('id' => $id))->save()) {
            $this->success('区域编辑成功！', U('Area'));
        } else {
            $this->error($Area->getError());
        }
    }

    //区域删除
    public function AreaDel($id = "") {

        IF ($id == "") {
            $this->error("区域刪除失败!");
        }
        $AreaData = array('status' => -1);
        $Area = M('Area');
        if ($Area->where(array('id' => $id))->save($AreaData)) {
            $this->success('区域删除成功！', U('Area'));
        } else {
            $this->error($Area->getError());
        }
    }

    //返回一组区域
    public function AjaxAreaList() {
        $AreaList = D('Area')->where("status=1")->select();
        $this->ajaxReturn($AreaList);
    }

    //返回一个区域
    public function AjaxArea($id) {
        $Area = D('Area')->where(array('id' => $id))->find();
        $this->ajaxReturn($Area);
    }
    
    //返回一组仓库数据
    public function AjaxWarehouseList(){
        $WarehouseList=D('warehouse')->where("status=1")->select();
        $this->ajaxReturn($WarehouseList);
    }
    
    //判断点在那个区域
    public function CheckPoint($lng,$lat){        
            $GeoUtils=new BMap\GeoUtils();
            $AID=$GeoUtils->isPointInArea($lng,$lat); 
            if($AID>0){
                 $Area = D('Area')->where(array('id' => $AID))->find();
                 $data['info']="点在区域：".$Area['areaName'];
            }else{
                 $data['info']="点不在任何区域内";
            }
             $this->ajaxReturn($data);
    }

    
    
    
    /******
     * 订单分区
     */
    
    public function changearea($ordertype,$orderid){
     
        
        $Model;
        if($ordertype==0){
            $Model=M('order');
        }else{
            $Model=M('gp_order');
        }
        $map['orderid']=$orderid;
        $orderdata=$Model->where($map)->find();
        
        $mapnext['createDate']  = array('GT',$orderdata['createDate']);
        $mapnext['areaId']=0;
        $nextorderid=$Model->where($mapnext)->getField("orderid");
        
        $arealist=M('area')->where('status=1')->select();
        
        $Count = M('order')->where('areaId=0')->count();
        $tCount = M('gp_order')->where('areaId=0')->count();
        
        $this->assign('data',$orderdata);
         $this->assign('nextorderid',$nextorderid);
        $this->assign('arealist',$arealist);
        $this->assign('Count',$Count);
          $this->assign('tCount',$tCount);
          $this->assign('ordertype',$ordertype);
        //dump($orderdata);
        $this->display();
    }
    
    //分区确定操作
    function changeareaaction($ordertype,$orderid,$areaid){
        if(IS_POST){
           if(isset($areaid) && isset($orderid)){
                $Model;
                if($ordertype==0){
                    $Model=M('order');
                }else{
                    $Model=M('gp_order');
                }
               $map['orderid']=$orderid;
               $data['areaId']=$areaid;
               $data['syncStatus']=2;
               $result=$Model->where($map)->save($data);
               if($result){
                   $this->success("订单分区手动设置成功！");
               }else{
                     $this->error("订单分区手动设置失败！");
               }
           }else{
                 $this->error("订单分区手动设置失败！");
           }
        }
    }

}
